package com.wzqj.agent.app.version.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.app.version.service.VersionService;
import com.wzqj.agent.util.Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

/**
 * Created by wanghao@weipass.cn on 2015/8/3.
 */
@Service("versionService")
public class VersionServiceImpl implements VersionService {

    private static Logger log = LogManager.getLogger();

    /**
     * 检查版本更新
     * @param mk
     * @param versionCode
     * @return
     */
    @Override
    public JSONObject check(String mt, String versionCode) {

        JSONObject data = null;
        try {
            String s = Util.readTxt("res/checkAppUpdate.json");
            JSONObject text = JSONObject.parseObject(s);
            JSONObject androidios = text.getJSONObject(mt);
            int must = androidios.getIntValue("mustversion");
            int newvc = androidios.getIntValue("newversion");
            int clientvc = Integer.parseInt(versionCode);
            String update = null;
            //如果 must < client < newest : 可升
            //如果 client <= must : 必升
            if(clientvc < newvc)	{
                if(clientvc <= must)	{
                    update = "m";
                } else if(clientvc > must){
                    update = "p";
                }
            } else	{
                update = "n";
            }
            data = new JSONObject();
            data.put("feature", text.getJSONObject("feature"));
            data.put("update", update);
            data.put("url", androidios.getString("url"));
            data.put("version", androidios.getString("version"));
            data.put("versioncode", androidios.getIntValue("newversion"));
            data.put("info", androidios.getString("info"));
        } catch (Exception e) {
            log.error("VersionServiceImpl#check:"+ e.getMessage());
        }
        return data;
    }
}
